package com.ia.bikerecife.speech;

import org.ispeech.FreeformType;
import org.ispeech.SpeechRecognizer;
import org.ispeech.SpeechRecognizerEvent;
import org.ispeech.SpeechResult;
import org.ispeech.SpeechSynthesis;
import org.ispeech.SpeechSynthesisEvent;
import org.ispeech.error.BusyException;
import org.ispeech.error.InvalidApiKeyException;
import org.ispeech.error.NoNetworkException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.util.Log;
import android.widget.Toast;

public class ISpeech {

    private static String    TAG        = "leac";
    private static String    VOICE_TYPE = "brportuguesefemale";
    private static String    LOCALE     = "pt-BR";

    private Context          mContext;
    private SpeechSynthesis  mSpeechSynthesis;
    private SpeechRecognizer mSpeechRecognizer;
    
    

    public ISpeech(Context context) {
        mContext = context;

        prepareTTSEngine();
       
        mSpeechSynthesis.setStreamType(AudioManager.STREAM_MUSIC);
        
    }

    
    public void StartSpeechRecognizer() {
        startRecognition();
    }

    public void StartSpeechSyntesis(String message) {
        try {
            //mSpeechSynthesis.setVoiceType(VOICE_TYPE);
            mSpeechSynthesis.speak(message);
        }
        catch (BusyException e) {
            Log.e(TAG, "SDK is busy");
            e.printStackTrace();
            Toast.makeText(mContext, "ERROR: SDK is busy", Toast.LENGTH_LONG).show();
        }
        catch (NoNetworkException e) {
            Log.e(TAG, "Network is not available\n" + e.getStackTrace());
            Toast.makeText(mContext, "ERROR: Network is not available", Toast.LENGTH_LONG).show();
        }
    }

    
        private void prepareTTSEngine() {
        try {
            mSpeechSynthesis = SpeechSynthesis.getInstance((Activity) mContext);
            mSpeechSynthesis.setVoiceType("brportuguesefemale");
            mSpeechSynthesis.setSpeechSynthesisEvent(new SpeechSynthesisEvent() {

                public void onPlaySuccessful() {
                    Log.i(TAG, "onPlaySuccessful");
                }

                public void onPlayStopped() {
                    Log.i(TAG, "onPlayStopped");
                }

                public void onPlayFailed(Exception e) {

                    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                    builder.setMessage("Error[TTSActivity]: " + e.toString()).setCancelable(false)
                            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                }
                            });
                    AlertDialog alert = builder.create();
                    alert.show();
                }

                public void onPlayStart() {
                    Log.i(TAG, "onPlayStart");
                }

                @Override
                public void onPlayCanceled() {
                    Log.i(TAG, "onPlayCanceled");
                }

            });

        }
        catch (InvalidApiKeyException e) {
            Log.e(TAG, "Invalid API key\n" + e.getStackTrace());
            Toast.makeText(mContext, "ERROR: Invalid API key", Toast.LENGTH_LONG).show();
        }
    }

    private void startRecognition() {
        setupFreeFormDictation();

        // recognizer.addOptionalCommand("model","assistant");
        try {
            mSpeechRecognizer.startRecord(new SpeechRecognizerEvent() {
                @Override
                public void onRecordingComplete() {  Log.v("leac", "4");
                    Log.i(TAG, "onRecordingComplete");
                }

                @Override
                public void onRecognitionComplete(SpeechResult result) {
                    Log.v(TAG, "Recognition complete");
                    if (result != null) {
                        Log.d(TAG, "Text Result:" + result.getText());
                    
                    }
                    else
                        Log.d(TAG, "Result is null...");
                }

                @Override
                public void onRecordingCancelled() {  Log.v("leac", "7");
                    Log.i(TAG, "onRecordingCancelled");
                }

                @Override
                public void onError(Exception exception) {  Log.v("leac", "8");
                    Log.e(TAG, exception.getMessage());
                    exception.printStackTrace();
                }
            });
        }
        catch (BusyException e) {
            e.printStackTrace();
        }
        catch (NoNetworkException e) {
            e.printStackTrace();
        }
    }

    private void setupFreeFormDictation() {
        try {
            mSpeechRecognizer = SpeechRecognizer.getInstance(mContext);
          
            mSpeechRecognizer.setFreeForm(FreeformType.FREEFORM_DICTATION);
            mSpeechRecognizer.setLocale(LOCALE);
        }
        catch (InvalidApiKeyException e) {
            e.printStackTrace();
        }
    }
}
